﻿/// <reference path="../ext-all.js" />

//自动选用Ext或者JQ进行通信
var JQAjax = {
    callServer: function (sUrl, sData, bAsync, sType, oCallBackFunction, vCallBackFunctionParameters) {
        /// <summary>与服务器通信</summary>
        /// <param name="sUrl" type="String">服务器处理请求的url路径</param>
        /// <param name="sData" type="String">post到服务器端的数据</param>
        /// <param name="bAsync" type="Boolean">是否异步请求；true：异步；false：同步（默认值）</param>
        /// <param name="sType" type="String">只能取两种值：get/post</param>
        /// <param name="oCallBackFunction" type="Function">回调的方法</param>
        /// <param name="vCallBackFunctionParameters" type="Object">传递给回调方法的参数</param>
        if (typeof (bAsync) == "undefined" || bAsync == null) bAsync = false; //默认采用同步的模式通信
        var sResult;
        jQuery.ajax({
            type: sType,
            url: sUrl,
            async: bAsync,
            data: sData,
            success: function (data, textStatus) {
                sResult = data;
                if (oCallBackFunction) {
                    oCallBackFunction(data, vCallBackFunctionParameters);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
        return sResult;
    }
};

//实现 MyAjax 接口
(function () { 
    if (typeof (MyAjax) != "undefined") {
        MyAjax.post = function (sUrl, sPostData, bAsync, oCallBackFunction, aCallBackFunctionParameters) {
            return JQAjax.callServer(sUrl, sPostData, bAsync, "POST", oCallBackFunction, aCallBackFunctionParameters);
        };

        MyAjax.get = function (sUrl, vParam, bAsync, oCallBackFunction, aCallBackFunctionParameters) {
            return JQAjax.callServer(sUrl, vParam, bAsync, "GET", oCallBackFunction, aCallBackFunctionParameters);
        };
    }    
})();
